<?php
exists_frame();
class base extends libdata{
    protected $db_instance = null;
    protected $tpl_instance = null;
    protected $app_name = null;
    public $cfg;
    private $free_instance = false;
    protected $dbm_obj_list = null;
    public $session = null;
    public $cookie = null;
    public $file_mime;
    public $visit_time = 0;
    public $visit_ip = 0;
    static public $function_list = array();
    public function __construct($cfg) {
        $this->visit_time = time();
        $this->visit_ip = get_uip();
        $this->dbm_obj_list = new data_class();
        $this->init_config($cfg);
        $this->init_session();
        $this->construct_standard_template_data();
        $this->declare_instance_list();
        $this->init_tpl();
    }
    public function run($method_name) {
        $re = call_user_func(array($this,$method_name));
        $this->T->app_name = $this->app_name;
        $this->tpl_instance->T = $this->T;
        $this->tpl_instance->_var = $this->D;
        $this->tpl_instance->fetch_templete();
        if(is_object($this->tpl_instance)) {
            $this->tpl_instance->display();
        }
    }
    public function construct_standard_template_data() {
        $this->D->site_name = $this->cfg->site_name;
    }
    public function init_config($cfg) {
        $this->cfg = $this->array2object($cfg);
    }
    public function initdb() {
        $this->db_instance = new dba();
    }
    public function init_session() {
        $this->session = new libsession();
        $this->cookie = new libcookie();
    }
    public function init_tpl() {
        $this->tpl_instance = new libtemplate($this->cfg);
    }
    public function get_data_property($name) {
        if(in_array($name,array_keys(get_class_vars('index'))) && in_array(CALL_PAGE_NAME,index::$function_list)) {
            return $this->$name;
        }
        return null;
    }
    public function load_data_operate_class($name, $arg='') {
        if(method_exists($this,$name) && in_array(CALL_PAGE_NAME,index::$function_list)) {
            return call_user_func_array(array($this,$name),$arg);
        }
        if(in_array($name,index::$function_list)) {
            include_once(CALL_PAGE_DIR . "/{$name}.php");
            array_unshift($arg,$this);
            return call_user_func_array($name,$arg);
        }
        $parts = explode('_',$name);    
        if($parts[0] == 'dbm') {
            $path = APP_ROOT . '/dbm';
        } else {
            return false;
        }
        $class_name = substr($name,4);
        $file_name = "$path/$class_name.php";
        if(file_exists($file_name)) {
            include_once($file_name);
            if(class_exists($name)) {
                $obj =  new $name($this->cfg);
                $this->dbm_obj_list->$name = $obj;
                return $obj;
            }
        }
        return false;
    }
    public function call_ulib($cls) {
        require_once(APP_ROOT .'/lib/'.$cls.'.php');
        $ulib = new $cls;
        $ulib->init_base_obj($this->cfg,$this->dbm_obj_list, $this->visit_time,$this->visit_ip);
        return $ulib;
       // $ulib = new $cls($this->cfg,$this->dbm_obj_list);
        //return $ulib;
    }
    public function check_file_type($file) {
        if(function_exists('finfo_open') && function_exists('finfo_file')) {
            $fif = finfo_open(FILEINFO_MIME_TYPE);
            $this->file_mime = finfo_file($fif,$file);
        } else {
            $this->file_mime = @mime_content_type($file);
        }
        return in_object($this->file_mime, $this->cfg->upfile_mime);
    }
    public function __destruct() {
        $this->db_instance = null;
        $this->tpl_instance = null;
   }
    public function check_post_method() {
        return $_SERVER['REQUEST_METHOD'] == 'POST';
    }
    public function get_ajax_data() {
        if(!$this->check_post_method()) return false;
        if(!$this->check_get('is_ajax')) return 0;
        return json_decode($_POST['data']);
    }
    public function check_ajax_data($idx) {
        $data = $this->get_ajax_data();
        return !empty($data->$idx);;
    }
    public function check_post($idx) {
        return filter_input(INPUT_POST,$idx,FILTER_NULL_ON_FAILURE);
    }
    public function check_get($idx) {
        return !empty($_GET[$idx]);
    }
    public function set_ajax($status, $message, $data = null) {
        $this->T->notpl = true;
        $this->tpl_instance->ajax_return_json = array('status'=>$status,'message'=>$message,'data'=>$data);
    }
    public function get_upload_file_auth($idx) {
        if(empty($this->cfg->upload_file_tmp_dir)) {
            throw new debug('unset upload file temp dir');
        }
        $v = sha1(randstr(6,10).time());
        if(!isset($this->session->upload_file_auth)) {
            $a = array($idx=>$v);
            $this->session->upload_file_auth = $a;
        } else {
            $value = $this->session->upload_file_auth;
            $value[$idx] = $v;
            $this->session->upload_file_auth = $value;
        }
    }
    public function destroy_upload_file_auth($idx) {
        unset($this->session->upload_file_auth[$idx]);
    }
    public function check_upload_file_auth($idx) {
        return isset($this->session->upload_file_auth[$idx]);
    }
    public function get_tmp_path() {
        $tmp_dir_first = substr($this->cfg->upload_file_tmp_dir,0,1);
        if($tmp_dir_first == '/') {
            $tmp_dir = $this->cfg->upload_file_tmp_dir;
        } else {
            if(substr($this->cfg->upload_file_tmp_dir,0,2) == './') {
                $tmp_dir = APP_ROOT .substr($this->cfg->upload_file_tmp_dir,1,-1);
            } else {
                $tmp_dir = APP_ROOT. '/'.$this->cfg->upload_file_tmp_dir;
            }
        }
        if(substr($tmp_dir,-1,1) != '/') {
            $tmp_dir .= '/';
        }
        $tmp_dir = str_replace('\\','/',$tmp_dir);
        return $tmp_dir;
    }
    public function upfile_tmp($idx) {
        if(!$this->check_upload_file_auth($idx)) return UNAUTH_ACCESS;
        if($_FILES[$idx]['error'] != UPLOAD_ERR_OK) return $_FILES[$idx]['error'];
        if(empty($_FILES[$idx]) || empty($_FILES[$idx]['tmp_name'])) return UPFILE_NOT_EXISTS;
        if($this->check_file_type($_FILES[$idx]['tmp_name']) == false) return UPFILE_FILE_TYPE_ERROR;
        if(($filesize = filesize($_FILES[$idx]['tmp_name'])) > $this->cfg->upfile_size) {
            @unlink($_FILES[$idx]['tmp_name']);
            return UPFILE_SIZE_LARGER;
        }
        $type = is_image($this->file_mime) ? 'images' : 'file';
        $suffix = ext($this->file_mime);
        $upfle_info = array();
        $upfle_info['src_name'] = $_FILES[$idx]['name'];
        $upfle_info['size'] = byte_format($filesize);
        $upfle_info['tmp_name'] = sha1(randstr(5,9).time().$upfle_info['src_name']) .'.'. $suffix;
        $tmp_dir = $this->get_tmp_path(); 
        $dest = $tmp_dir . $upfle_info['tmp_name'];
        $upfle_info['tmp_path'] = $dest;
        if(@move_uploaded_file($_FILES[$idx]['tmp_name'], $dest)) {
            $this->destroy_upload_file_auth($idx);
            return $upfle_info;
        }
        return UPFILE_FAILURE;
    }
    public function upload_file($idx) {
        if(empty($_FILES[$idx])) {
            return false;
        } else if(empty($_FILES[$idx]['tmp_name'])) {
            throw new debug('缓存文件不存在');
            return false;
        } else if($this->check_file_type($_FILES[$idx]['tmp_name'])){
            $type = is_image($this->file_mime) ? 'images' : 'file';
            $suffix = ext($this->file_mime);
            $dir =  $this->cfg['upload_dir'] . '/' . $type;
            if(!is_dir(WEB_ROOT. '/'. $dir)) {
                mkdir(WEB_ROOT .'/'. $dir);
            }
            if(filesize($_FILES[$idx]['tmp_name']) > $_ENV['cfg']['upfile_size']) {
                @unlink($_FILES[$idx]['tmp_name']);
                $this->upfile_error = '上传文件过大';
                return false;
            }
            if(file_exists($dir . '/' . 'LASTEST')) {
                $dir_num = file_get_contents($dir .'/'.'LASTEST');
            } else {
                $dir_num = 0;
                mkdir(WEB_ROOT .'/'. $dir . '/' . $dir_num);
                file_put_contents(WEB_ROOT . '/' . $dir . '/' . 'LASTEST', '0');
                file_put_contents(WEB_ROOT . '/' . $dir . '/' .$dir_num .'/' . 'LASTEST', '0');
            }
            $file_num = file_get_contents($dir. '/' . $dir_num .'/' . 'LASTEST');
            if($file_num >= 3000) {
                $dir_num++;
                mkdir(WEB_ROOT . '/'.$dir . '/' . $dir_num);
                file_put_contents(WEB_ROOT .'/'.$dir . '/'.$dir_num .'/' . 'LASTEST',0);
            }
            $dir = $dir . '/'. $dir_num;
            $rand_file = md5(randstr(5,10). '/' . $_FILES[$idx]['name']);
            $new_file_name =  '/'.$dir . '/' . $rand_file . '.'.$suffix;
            $dest = WEB_ROOT . '/' . $new_file_name;
            if(@move_uploaded_file($_FILES[$idx]['tmp_name'], $dest)) {
                $source_name = conv_quotation($_FILES[$idx]['name']);
                return array('source_name'=>$source_name, 'web_path'=>$new_file_name, 'absolute_path'=> $dest, 'mime'=> $this->file_mime);
            } else {
                $_ENV['debug']['error_type'] = 'upload_file';
                $_ENV['debug']['debug_backtrace'] = debug_backtrace();
                $_ENV['debug']['error_info'] = '移动上传文件错误';
                $this->upfile_error = '移动上传文件错误';
                return false;
            }
        }
    }
}

